package com.joey.cloud.auth.service;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.joey.cloud.auth.form.PwdLoginForm;
import com.joey.cloud.auth.form.PwdRegisterForm;
import com.joey.cloud.auth.sys.entity.SysAccount;
import com.joey.cloud.auth.sys.service.ISysAccountService;
import com.joey.cloud.communal.constant.BaseConstant;
import com.joey.cloud.communal.constant.CaffeineConstant;
import com.joey.cloud.communal.utils.JoeDevUtils;
import com.joey.cloud.communal.utils.JwtUtils;
import com.joey.cloud.communal.utils.ResponseUtil;
import com.joey.cloud.provider.service.BaseProviderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 授权-密码模式-业务
 */
@Service
public class AuthPwdService {

    @Resource
    ISysAccountService sysAccountService;
    @Resource
    BaseProviderService baseProviderService;

    /**
     * 登录
     * @param form
     * @return
     */
    public ResponseUtil pwdLogin(PwdLoginForm form) {
        SysAccount account =  sysAccountService.queryByName(form.getAccountNum());
        //校验密码
        if(account==null||!SecureUtil.md5(form.getPassword().trim()).equals(account.getPassword())){
            return ResponseUtil.noAuthority5();
        }
        if(account.getStatusId().equals(BaseConstant.int0)){
            return ResponseUtil.error("账号已禁用");
        }
        if(account.getPsnId()==null){
            return ResponseUtil.error("账号已失效");
        }
        //生成token
        String token = JwtUtils.generateToken(account.getPsnId());
        //构建用户权限信息
        ResponseUtil res = baseProviderService.getPsnFeignService().queryPsnAuthInfo(account.getPsnId());
        //token放到缓存中
        String jsonStr = JSONObject.toJSONString(res.getData());
        //baseProviderService.getCacheFeignService().redisStrSet(BaseConstant.AUTH_PWD +account.getPsnId(),jsonStr);
        baseProviderService.getCacheFeignService().caffeineSet(CaffeineConstant.cacheAuth,BaseConstant.AUTH_PWD +account.getPsnId(),jsonStr);
        return ResponseUtil.success(token);
    }

    /**
     * 注册
     * @param form
     * @return
     */
    @GlobalTransactional
    public ResponseUtil pwdRegister(PwdRegisterForm form) {
        SysAccount account =  sysAccountService.queryByName(form.getAccountNum());
        if(account!=null){
            return ResponseUtil.error("账号名已存在");
        }
        ResponseUtil res = baseProviderService.getPsnFeignService().insertPsn(form);
        if(!res.getSuccess()){
            return ResponseUtil.error("psn服务创建用户失败");
        }
        JSONObject json = JoeDevUtils.obj2Json(res.getData());
        Long psnId = json.getLong("id");
        sysAccountService.saveByRegister(psnId,form);
        return ResponseUtil.success();
    }


    /**
     * 获取个人信息
     * @return
     */
    public ResponseUtil queryUserInfo() {
        Long psnId = JwtUtils.getCurrentUserIdNoException();
        ResponseUtil res = baseProviderService.getPsnFeignService().getPerson(psnId);
        return res;
    }
}
